<?php
/**
 * \class HumanProve
 *  Creates and checks a string to prove you're a human.
 *  Check the MIT License for details on usage. 
 *  
 * Copyright (c) 2010 WebsiteGuys.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. 
 *
 * \author Wes Alvaro
 * \date 2010-02-04  
 *
 **/    


/****** Customizations **************************/

/// Binary operators that must be recognized by PHP.
define('OPS','+-');
/// Must be the same length as OPS, but URL friendly.
define('REP','ab'); 
/// Max random number to generate.
define('MAX',10);
/// Min random number to generate.
define('MIN',10);

/************************************************/

class humanProve {
  var $equation;
  var $equationURL;
  var $val;
  static $num = 0;
  function __construct(){
  }
  function generate(){
    $op1 = rand(MIN,MAX);
    $op2 = rand(MIN,MAX);
    $rop = rand(0,strlen(OPS)-1);
    $op  = substr(OPS, $rop,1);
    $urlOp  = substr(REP, $rop,1);
    $this->equation = "$op1 $op $op2";
    $this->equationURL = "$op1$urlOp$op2";
    $this->val = eval("return ($op1 $op $op2);");
    return array( $this->equation, $this->val, $this->equationURL);
  }
  function checkForm(){
    $data = $_POST;
    if(!isset($data['humanProveQ'])) return false;
    if(!isset($data['humanProveA'])) return false;
    return $this->check($data['humanProveQ'],$data['humanProveA']);
  }
  function check($str = '', $val){
    $str = preg_replace('/[^a-z0-9]/i', '', trim($str));
    $val = intval($val);
    if(!strlen($str)) return false;
    for($i = 0; $i < strlen(OPS); $i++){
      $op = substr(OPS, $i,1);
      $urlOp = substr(REP, $i,1);
      $str = str_replace($urlOp, $op, $str);
    }
    if(eval("return ($str);") == $val) return true;
    else return false;
  }
  function view(){
    ++$this->num;
  ?>
    <div class="captcha">
       <label for="hp<?php echo $this->num	?>"><?php echo $this->equation ?> = </label>
       <input type="hidden" name="humanProveQ" value="<?php echo $this->equationURL ?>" />
       <input type="text" size="3" id="hp<?php echo $this->num	?>" name="humanProveA" />
    </div>
  <?php
  }
}
?>